package com.pkutech.carcasher.db;

import com.pkutech.carcasher.model.HmSelectFormalOrder;
import com.pkutech.carcasher.model.IllegalPark;
import com.pkutech.carcasher.model.MsgDB;
import com.pkutech.carcasher.model.ParkingLog;
import com.pkutech.carcasher.model.RoadInfo;
import com.pkutech.carcasher.model.SelectBookingOrder;
import com.pkutech.carcasher.model.SelectFormalOrder;
import com.pkutech.carcasher.model.UserInfo;
import java.util.List;
import io.realm.Realm;
import io.realm.Sort;

/**
 * Created by cyy on 2016/6/19.
 */
public class RealmManager {

    /**
     * 非法停车
     */
    public static void saveIllegalParks(List<IllegalPark> illegalParks){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(illegalParks);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<IllegalPark> findAllIllegalParks(){
        Realm.getDefaultInstance().beginTransaction();
        List<IllegalPark> illegalParks = Realm.getDefaultInstance().where(IllegalPark.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  illegalParks;
    }

    public static void clearIllegalParks() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(IllegalPark.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    /**
     * 历史停车
     */
    public static void saveParkingLogs(List<ParkingLog> parkingLogs){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(parkingLogs);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<ParkingLog> findAllParkingLogs(){
        Realm.getDefaultInstance().beginTransaction();
        List<ParkingLog> parkingLogs = Realm.getDefaultInstance().where(ParkingLog.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  parkingLogs;
    }

    public static void clearParkingLogs() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(ParkingLog.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    /**
     * 预订单
     */
    public static void saveSelectBookingOrders(List<SelectBookingOrder> selectBookingOrders){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(selectBookingOrders);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<SelectBookingOrder> findAllSelectBookingOrders(){
        Realm.getDefaultInstance().beginTransaction();
        List<SelectBookingOrder> parkingLogs = Realm.getDefaultInstance().where(SelectBookingOrder.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  parkingLogs;
    }

    public static void clearSelectBookingOrders() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(SelectBookingOrder.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    /**
     * 正式订单
     */
    public static void saveSelectFormalOrders(List<SelectFormalOrder> selectFormalOrders){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(selectFormalOrders);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<SelectFormalOrder> findAllSelectFormalOrders(){
        Realm.getDefaultInstance().beginTransaction();
        List<SelectFormalOrder> selectFormalOrders = Realm.getDefaultInstance().where(SelectFormalOrder.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  selectFormalOrders;
    }
    public static void clearSelectFormalOrders() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(SelectFormalOrder.class);
        Realm.getDefaultInstance().commitTransaction();
    }
    /**
     * hm正式订单
     */
    public static void saveHmSelectFormalOrders(List<HmSelectFormalOrder> selectFormalOrders){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(selectFormalOrders);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<HmSelectFormalOrder> findHmAllSelectFormalOrders(){
        Realm.getDefaultInstance().beginTransaction();
        List<HmSelectFormalOrder> selectFormalOrders = Realm.getDefaultInstance().where(HmSelectFormalOrder.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  selectFormalOrders;
    }

    public static void clearhMSelectFormalOrders() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(HmSelectFormalOrder.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    /**
     * 停车场
     */
    public static void saveRoads(List<RoadInfo> roadInfos){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(roadInfos);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<RoadInfo> findAllRoads(){
        Realm.getDefaultInstance().beginTransaction();
        List<RoadInfo> roadInfos = Realm.getDefaultInstance().where(RoadInfo.class).findAll();
        Realm.getDefaultInstance().commitTransaction();
        return  roadInfos;
    }

    public static void clearRoads() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(RoadInfo.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    /**
     * 用户信息
     */
    public static void saveUserInfo(UserInfo userInfo){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(userInfo);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static UserInfo findUserInfo(){
        Realm.getDefaultInstance().beginTransaction();
        UserInfo userInfo = Realm.getDefaultInstance().where(UserInfo.class).findFirst();
        Realm.getDefaultInstance().commitTransaction();
        return  userInfo;
    }

    public static void clearUserInfo() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(UserInfo.class);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static void saveMessage(MsgDB message){
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(message);
        Realm.getDefaultInstance().commitTransaction();
    }

    public static List<MsgDB> findAllMessage(){
        Realm.getDefaultInstance().beginTransaction();
        List<MsgDB> messageList = Realm.getDefaultInstance().where(MsgDB.class).findAllSorted("time", Sort.DESCENDING);
        Realm.getDefaultInstance().commitTransaction();
        return  messageList;
    }

    public static void deleteMessageByTime(long time){
        Realm.getDefaultInstance().beginTransaction();
        MsgDB msgDB = Realm.getDefaultInstance().where(MsgDB.class).equalTo("time", time).findFirst();
        msgDB.deleteFromRealm();
        Realm.getDefaultInstance().commitTransaction();
    }

    public static void clearMessage() {
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().delete(MsgDB.class);
        Realm.getDefaultInstance().commitTransaction();
    }

}
